package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {
    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    /*
    * 查通用设置
    * */
    @GetMapping("/settings")
    public ResponseEntity settings(){
        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);
    }

    /*
    * 设置陌生人问题
    * */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        //获取参数
        String content = (String) map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    /*
    * 通知设置
    * */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map){
        //获取参数
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

    /*
    * 分页查询黑名单列表
    * */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        //1、调用service查询
        PageResult pr = settingsService.blacklist(page,size);
        //2、构造返回
        return ResponseEntity.ok(pr);
    }

    /*
     * 移除黑名单
     * */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid")Long blackUserId){
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }


    /*
    * 修改手机号 1.发送验证码
    * */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity  sendVerificationCode(){
        //首先拿到前端给的手机号码
        String mobile = UserHolder.getMobile();
        //再通过UserService调用方法发送验证码
        userService.sendMsg(mobile);

        return ResponseEntity.ok(null);
    }

    /*
    * 修改手机号  2.校验验证码
    * */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
       Map verification = settingsService.checkVerificationCode(map);
       return ResponseEntity.ok(verification);
    }

    /*
    * 修改手机号 3.保存
    * */
    @PostMapping("/phone")
    public ResponseEntity updateMobile(@RequestBody Map map){
        userService.updateMobile(map);
        return ResponseEntity.ok(null);
    }
}
